// miniprogram/pages/my/home/home.js
const app = getApp();
const AV = require('../../../libs/leancloud/av-weapp-min.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        version: 'v0.1.0',
        headerBGURL: 'https://ae01.alicdn.com/kf/H159be3b332024195917804fee84df459I.png',
        headerWaveURL: 'https://ae01.alicdn.com/kf/H68904fe3a6264caa8a2357e1dc7f2014c.png',
        praiseQRCodeURL: 'https://gitee.com/yicm/Images/raw/master/common/wx_appreciation_code.jpg',
        shareBGURL: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1896986724,1272849223&fm=26&gp=0.jpg',

        menuItems: [
            {
                id: -2,
                icon: "/images/subscribe.svg",
                content: "订阅文章",
                url: "",
                method: "requestSubscribeMessage",
                show: true,
                open_type: ''
            },
            {
                id: -1,
                icon: "/images/icon-images.svg",
                content: "当前加载页图片集",
                url: "",
                method: "launchImages",
                show: true,
                open_type: ''
            },
            {
                id: 0,
                icon: "/images/icon-feet.svg",
                content: "浏览记录",
                url: "",
                method: "myHistory",
                show: true,
                open_type: ''
            },
            {
                id: 1,
                icon: "/images/icon-collection.svg",
                content: "我的收藏",
                url: "",
                method: "myHistory",
                show: true,
                open_type: ''
            },
            {
                id: 2,
                icon: "/images/icon-github-blue.svg",
                content: "Github",
                url: "",
                method: "",
                show: true,
                open_type: ''
            },
            {
                id: 3,
                icon: "/images/icon-caffe.svg",
                content: "请喝咖啡",
                url: "",
                method: "showQRcode",
                show: true,
                open_type: ''
            },
            {
                id: 4,
                icon: "/images/icon-share.svg",
                content: "分享",
                url: "",
                method: "",
                show: true,
                open_type: 'share'
            },
            {
                id: 6,
                icon: "/images/icon-feedback.svg",
                content: "意见反馈",
                url: "",
                method: "",
                show: true,
                open_type: 'feedback'
            },
            {
                id: 7,
                icon: "/images/icon-wechat.svg",
                content: "加微信进群",
                url: "",
                method: "contactMe",
                show: true,
                open_type: ""
            },
            {
                id: 10,
                icon: "/images/icon-link.svg",
                content: "申请友链",
                url: "",
                method: "friendLink",
                show: false,
                open_type: ""
            },
            {
                id: 5,
                icon: "/images/icon-release-log.svg",
                content: "更新日志",
                url: "",
                method: "changeLog",
                show: true,
                open_type: ''
            },
            {
                id: 9,
                icon: '/images/icon-version.svg',
                content: "版本",
                url: "",
                method: "version",
                show: false,
                open_type: ''
            },        
            {
                id: 8,
                icon: "/images/icon-about.svg",
                content: "关于我",
                url: "",
                method: "about",
                show: true,
                open_type: ''
            },
            {
                id: 11,
                icon: "/images/icon-disclaimer.svg",
                content: "免责声明",
                url: "",
                method: "disclaimer",
                show: true,
                open_type: ''
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;

        if (app.globalData.config) {
            that.data.headerBGURL = app.globalData.config.my.bg;
            that.data.headerWaveURL = app.globalData.config.my.wave;

            that.data.praiseQRCodeURL = app.globalData.config.global.praise_qr_code;
            that.data.shareBGURL = app.globalData.config.global.share_bg;
            if (app.globalData.config.home_menu_items) {
                that.data.menuItems = app.globalData.config.home_menu_items;
            }

            that.setData({
                headerBGURL: that.data.headerBGURL,
                headerWaveURL: that.data.headerWaveURL,
                menuItems: that.data.menuItems
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let that = this;
        return {
            title: 'xsb的博客',
            path: '/pages/index/index',
            imageUrl: that.data.shareBGURL
        }
    },
    requestSubscribeMessage: function (e) {
        wx.requestSubscribeMessage({
            tmplIds: ['eWJacnVoyNj7nd-GeTZNXb0GixaP0eXNkDP3FMH7Uiw'],
            success(res) {
                wx.showToast({
                    title: '订阅文章成功',
                    duration: 2000
                })
                // 写入openid
                const user = AV.User.current();
                app.updateUserSubscribe(user.id,
                    user.attributes.authData.lc_weapp.openid,
                    user.attributes.authData.lc_weapp.session_key,
                    true, 'blog')
            }, fail(e) {
                console.log(e)
                wx.showToast({
                    title: '订阅失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    showQRcode: function (e) {
        let that = this;
        wx.previewImage({
            urls: [that.data.praiseQRCodeURL],
            current: that.data.praiseQRCodeURL
        })
    },
    myHistory: function (e) {
        let that = this;
        let content = e.currentTarget.dataset.content;
        if (content == '浏览记录') {
            // 浏览记录
            wx.navigateTo({
                url: '../history/history?index=0&content=' + content,
            })
        } else if (content == '我的收藏') {
            // 我的收藏
            wx.navigateTo({
                url: '../history/history?index=1&content=' + content,
            })
        } else {
            // not support
        }
    },
    contactMe: function() {
        wx.setClipboardData({
            data: 'hstxxsb',
            success(res) {
                 
            }
        })
    },
    about: function() {
        wx.navigateTo({
            url: '../../mdshow/mdshow?id=about&title=关于&url=https://gitee.com/api/v5/repos/hsxsb/blog_xsb/contents/hexoapp/source/_data/target/about.md&enable_comment=true'
        })
    },
    changeLog: function() {
        wx.navigateTo({
            url: '../../mdshow/mdshow?id=changelog&title=升级日志&url=https://gitee.com/api/v5/repos/hsxsb/blog_xsb/contents/hexoapp/source/_data/target/changelog.md&enable_comment=false'
        })
    },
    version: function() {
        wx.showToast({
            title: this.data.version,
            icon: 'none',
            duration: 3000
        })
    },
    friendLink: function() {
        wx.navigateTo({
            url: '../links/links',
        })
    },
    disclaimer: function() {
        wx.navigateTo({
            url: '../../mdshow/mdshow?id=disclaimer&title=免责声明&url=https://gitee.com/api/v5/repos/hsxsb/hsxsb/contents/source/_data/target/disclaimer.md&enable_comment=false'
        })
    },
    launchImages: function () {
        wx.navigateTo({
            url: '/pages/launch/launch?only_show=true'
        })
    }
})